package com.tqw.client.controller;

import com.tqw.auth.feign.AuthFegin;
import com.tqw.client.pojo.ClientPassword;
import com.tqw.client.service.ClientService;
import com.tqw.client.vo.ClientVO;
import com.tqw.constant.SecurityConstants;
import com.tqw.entity.JWTUtil;
import com.tqw.entity.Result;
import com.tqw.pojo.Client;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;


import java.time.Duration;
import java.util.concurrent.TimeUnit;

import static com.tqw.entity.StatusCode.ERROR;
import static com.tqw.entity.StatusCode.OK;

/**
 * controller类
 * @author Will
 */
@RestController
@RequestMapping("/client")
public class ClientController {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Autowired
    private ClientService clientService;

    @Autowired
    private AuthFegin authFegin;

    /**
     * 需求: 查询用户 通过id
     * 请求类型: get
     * URL:  /client/selectUserById?id=#{id}
     * 参数: id
     * @return Result<Client>
     */
    @GetMapping("/selectUserById")
    public Result<Client> selectUserById(@RequestHeader(SecurityConstants.AUTH_TOKEN)String token){
        Client selectClient = clientService.selectUserById(JWTUtil.getId(token));
        if(selectClient==null){
            return new Result<>(false, ERROR, "用户不存在");
        }
        return new Result<>(true, OK, "查询成功", selectClient);
    }

    /**
     * 需求: 修改个人信息 form表单
     * 请求类型: post
     * 请求路径: "/client/updateInformation"
     * 参数：from表单
     * @return Result
     */
    @PostMapping("/updateInformation")
    public Result<Client> updateInformation(@RequestBody Client client, @RequestHeader(SecurityConstants.AUTH_TOKEN)String token){
        client = JWTUtil.getClient(token).setEmail(client.getEmail()).setNickname(client.getNickname()).setPhone(client.getPhone());
        Client updateClient = clientService.updateInformation(client);
        if(updateClient==null){
            return new Result<>(false, ERROR, "修改失败");
        }
        redisTemplate.delete(token);
        token = JWTUtil.sign(JWTUtil.getMap(updateClient));
        redisTemplate.opsForValue().set(token,String.valueOf(client.getId()), Duration.ofHours(3));
        ClientVO clientVO = new ClientVO(updateClient,token);
        return new Result<>(true, OK, "修改成功", clientVO);
    }

    /**
     * 需求: 检查输入的原密码是否正确
     * 请求类型: post
     * 请求路径: "/client/checkPassword"
     * 参数：String password
     * @return Result<Client>
     */
    @PostMapping("/checkPassword")
    public Result<Client> checkPassword(@RequestParam String password,@RequestHeader(SecurityConstants.AUTH_TOKEN) String token){
        if(!clientService.checkPassword(password,token)){
            return new Result<>(false, ERROR, "旧密码错误");
        }
        return new Result<>(true, OK, "查询成功", true);
    }

    /**
     * 需求: 比对（输入新密码）和（再次输入新密码）
     * 请求类型: post
     * 请求路径: "/client/contrastPassword"
     * 参数：ClientPassword clientPassword
     * @return Result<Client>
     */
    @PostMapping("/contrastPassword")
    public Result<Client> contrastPassword(ClientPassword clientPassword){
        if(!clientService.contrastPassword(clientPassword)){
            return new Result<>(false, ERROR, "两次输入密码不匹配或为空");
        }
        return new Result<>(true, OK, "两次输入密码正确", true);
    }

    /**
     * 需求: 修改密码
     * 请求类型: post
     * 请求路径: "/client/updatePassword"
     * 参数：ClientPassword clientPassword
     * @return Result<Client>
     */
    @PostMapping("/updatePassword")
    public Result<Client> updatePassword(@RequestBody ClientPassword clientPassword,@RequestHeader(SecurityConstants.AUTH_TOKEN) String token){
        if(!clientService.updatePassword(clientPassword,token)){
            return new Result<>(false, ERROR, "修改失败");
        }
        return new Result<>(true, OK, "修改成功", true);
    }
}
